﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Common;
using PM.Services;

namespace PM.Site.Admin
{
    public partial class DataRead : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            } 
            
        }

        private void Loading()
        {
        
        }

        protected void btnLoad_Click(object sender, EventArgs e)
        {
            lblError.Text = "";

            if (fileUpDailyFile.HasFile)
            {
                try
                {
                    if (fileUpDailyFile.PostedFile.ContentType == "text/plain")
                    {
                        FileLoadServices fileObj = new FileLoadServices();
                        fileUpDailyFile.SaveAs(Server.MapPath("~/") + fileUpDailyFile.FileName);
                        
                        fileObj.LoadFile("PM.Site/"+fileUpDailyFile.FileName);
                        
                        lblLine.Text = fileObj.AllLines.ToString() + " lines.";

                        bulSummary.Items.Add(fileObj.TradeLines.ToString() + " Trades added.");
                        bulSummary.Items.Add(fileObj.OrderLines.ToString() + " Orders added.");
                        bulSummary.Items.Add(fileObj.TrsumLines.ToString() + " Transation added." );
                        bulSummary.Items.Add(fileObj.CDSLines.ToString() + "Direct CDS Transactions added.");

                        File.Delete("PM.Site/"+fileUpDailyFile.FileName);
                    }
                    else
                    {
                        lblError.Text = "Upload failed: Only text files are accepted!";       
                    }                 
                }
                catch (Exception exp)
                {
                    lblError.Text = "Upload failed: " + exp.Message;   
                }
                
            }
        }
    }
}
